<div *ngIf="!tokens" class="tw-items-center tw-justify-center tw-pt-64 tw-text-center">
  <i class="bwi bwi-spinner bwi-spin bwi-3x"></i>
</div>

<bit-no-items *ngIf="tokens?.length == 0">
  <ng-container slot="title">{{ "accessTokensNoItemsTitle" | i18n }}</ng-container>
  <ng-container slot="description">{{ "accessTokensNoItemsDesc" | i18n }}</ng-container>
  <button
    type="button"
    slot="button"
    bitButton
    buttonType="secondary"
    (click)="newAccessTokenEvent.emit()"
  >
    <i class="bwi bwi-plus" aria-hidden="true"></i>
    {{ "newAccessToken" | i18n }}
  </button>
</bit-no-items>

<bit-table *ngIf="tokens?.length >= 1">
  <ng-container header>
    <tr>
      <th bitCell class="tw-w-0">
        <label class="tw-m-0 tw-flex tw-w-fit tw-gap-2 !tw-font-bold !tw-text-muted">
          <input
            type="checkbox"
            (change)="$event ? toggleAll() : null"
            [checked]="selection.hasValue() && isAllSelected()"
            [indeterminate]="selection.hasValue() && !isAllSelected()"
          />
          {{ "all" | i18n }}
        </label>
      </th>
      <th bitCell>{{ "name" | i18n }}</th>
      <th bitCell>{{ "expires" | i18n }}</th>
      <th bitCell>{{ "lastEdited" | i18n }}</th>
      <th bitCell class="tw-w-0">
        <button
          type="button"
          bitIconButton="bwi-ellipsis-v"
          buttonType="main"
          [bitMenuTriggerFor]="tableMenu"
          [title]="'options' | i18n"
          [attr.aria-label]="'options' | i18n"
        ></button>
      </th>
    </tr>
  </ng-container>
  <ng-template body>
    <tr bitRow *ngFor="let token of tokens">
      <td bitCell>
        <input
          type="checkbox"
          (change)="$event ? selection.toggle(token.id) : null"
          [checked]="selection.isSelected(token.id)"
        />
      </td>
      <td bitCell>{{ token.name }}</td>
      <td bitCell>
        {{ token.expireAt === null ? ("never" | i18n) : (token.expireAt | date: "medium") }}
      </td>
      <td bitCell>{{ token.revisionDate | date: "medium" }}</td>
      <td bitCell>
        <button
          type="button"
          bitIconButton="bwi-ellipsis-v"
          buttonType="main"
          [title]="'options' | i18n"
          [attr.aria-label]="'options' | i18n"
          [bitMenuTriggerFor]="tokenMenu"
        ></button>
      </td>

      <bit-menu #tokenMenu>
        <button type="button" bitMenuItem (click)="revokeAccessTokensEvent.emit([token])">
          <span class="tw-text-danger">
            <i class="bwi bwi-fw bwi-minus-circle" aria-hidden="true"></i>
            {{ "revokeAccessToken" | i18n }}
          </span>
        </button>
      </bit-menu>
    </tr>
  </ng-template>
</bit-table>

<bit-menu #tableMenu>
  <button type="button" bitMenuItem (click)="revokeSelected()">
    <span class="tw-text-danger">
      <i class="bwi bwi-fw bwi-minus-circle" aria-hidden="true"></i>
      {{ "revokeAccessTokens" | i18n }}
    </span>
  </button>
</bit-menu>
